package edu.yngsxy.demo.controller;

import edu.yngsxy.demo.model.Result;
import edu.yngsxy.demo.model.TProduct;
import edu.yngsxy.demo.service.ProductService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Validated
public class ProductController {

    @Autowired
    private ProductService productService;


    /**
     * 添加商品
     * @param tProduct
     * @return
     */
    @PostMapping("/addProduct")
    public Result addProduct(@Validated @RequestBody TProduct tProduct){
        if(productService.addProduct(tProduct)){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败，请检查数据是否正确");
        }
    }

    /**
     * 查看商品详情
     * @return
     */
    @GetMapping("/getProductDetail")
    public Result<TProduct> getProductDetail(int productid){
        TProduct tProduct = productService.getProductDetail(productid);
        return Result.success(tProduct);
    }


    /**
     * 查看所有商品
     * @param productname
     * @param brand
     * @return
     */
    @GetMapping("/getProductList")
    public Result<List<TProduct>> getProductList(String productname,String brand){
        List<TProduct> list = productService.getProductList(productname,brand);
        return Result.success(list);
    }


    /**
     * 修改商品
     * @param tProduct
     * @return
     */
    @PutMapping("/modProduct")
    public Result modProduct( @Validated @RequestBody TProduct tProduct){
        System.out.println(tProduct+"-----------------------------------------------------");
        if(productService.modProduct(tProduct)){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败，请检查数据");
        }
    }

    @DeleteMapping("/deleteProduct")
    public Result deleteProduct(int productid){
        if(productService.deleteProduct(productid)){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    @GetMapping("/productAll")
    public Result<List<TProduct>> productAll(Integer userid){
        List<TProduct> list = productService.productAll(userid);
        return Result.success(list);
    }

}
